package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.BaseAttributeService;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.simpleframework.xml.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Tag(name = "分类管理")
@RestController
@RequestMapping(value="/api/album/category")
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryApiController {
	
	@Autowired
	private BaseCategoryService baseCategoryService;

	@Autowired
	private BaseAttributeService baseAttributeService ;

	/*查询所有分类数据*/
	//返回一个list 数据时AlbumCategoryVo
	@GetMapping(value = "/getBaseCategoryList")
	public Result<List<AlbumCategoryVo>> getBaseCategoryList(){
		//调用服务层的查询分类方法
		List<AlbumCategoryVo> albumCategoryVoList = baseCategoryService.findAllCategory();
		return Result.build(albumCategoryVoList, ResultCodeEnum.SUCCESS);
	}

	// /api/album/category/findAttribute/{category1Id}
	@GetMapping(value = "/findAttribute/{category1Id}")
	public Result<List<BaseAttribute>> findAttributeByC1Id(@PathVariable(value = "category1Id") Long category1Id) {
		List<BaseAttribute> attributeList = baseAttributeService.findAttributeByC1Id(category1Id) ;
		return Result.ok(attributeList) ;
	}

	// 加入了查询缓存的功能，并且使用了分布式锁解决了缓存击穿问题
	@GetMapping(value = "/findCategoryByC3Id/{category3Id}")
	public Result<BaseCategoryView> findCategoryByC3Id(@PathVariable(value = "category3Id") Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryService.findCategoryByC3Id(category3Id);
		return Result.ok(baseCategoryView) ;

	}

	// 根据一级分类的id查询分类数据
	@GetMapping(value = "/getBaseCategoryList/{category1Id}")
	public Result<AlbumCategoryVo> getBaseCategoryList(@PathVariable(value = "category1Id") Long category1Id) {
		AlbumCategoryVo albumCategoryVo = baseCategoryService.getBaseCategoryList(category1Id) ;
		return Result.ok(albumCategoryVo) ;
	}

	@GetMapping(value = "/findTopBaseCategory3/{category1Id}")
	public Result<List<BaseCategory3>> findTopBaseCategory3(@PathVariable(value = "category1Id") Long category1Id) {
		List<BaseCategory3> baseCategory3List = baseCategoryService.findTopBaseCategory3(category1Id) ;
		return Result.ok(baseCategory3List) ;
	}


}

